/*
* $Id: BQManagerDialog.cpp 153 2010-09-26 17:33:07Z volok-aleksej@yandex.ru $
*
* Copyright 2009 by Alexey Volokitin
* All rights reserved
*
*/
#include "Common/include.h"
#include "BQManagerDialog.h"
#include "QBManagerDialog/DialogManipulationWin.h"
#include "Components/Journal/QueenBeeManager.h"
#include "Components/Journal/FamilyManager.h"

CBQManagerDialog::CBQManagerDialog(CWnd* pParent, type tp, int nId)
: CDialog(IDD_BQ_MANAGE, pParent), m_type(tp), m_nId(nId){
	m_pGLWnd = new CDialogManipulationWin(this);
	m_pQBWnd = new CDialogManipulationWin(this);
}
CBQManagerDialog::~CBQManagerDialog(){
	delete m_pGLWnd;
	delete m_pQBWnd;
}

BOOL CBQManagerDialog::OnInitDialog(){
	BOOL bRes = __super::OnInitDialog();
	SetWindowText(ILanguageManager::GetLanguageManager()->GetString(QB_DIALOG_CAPTION_ID).c_str());
	m_close.SetWindowText(ILanguageManager::GetLanguageManager()->GetString(CLOSED_ID).c_str());
	m_new.SetWindowText(ILanguageManager::GetLanguageManager()->GetString(NEW_ID).c_str());
	m_update.EnableWindow(FALSE);
	m_update.SetWindowText(ILanguageManager::GetLanguageManager()->GetString(CHANGE_ID).c_str());
	m_delete.EnableWindow(FALSE);
	m_delete.SetWindowText(ILanguageManager::GetLanguageManager()->GetString(DELETE_ID).c_str());
	m_save.ShowWindow(SW_HIDE);
	m_save.SetWindowText(ILanguageManager::GetLanguageManager()->GetString(SAVE_ID).c_str());
	m_cancel.ShowWindow(SW_HIDE);
	m_cancel.SetWindowText(ILanguageManager::GetLanguageManager()->GetString(CANCEL_ID).c_str());
	m_changeF.EnableWindow(FALSE);
	m_changeF.SetWindowText(ILanguageManager::GetLanguageManager()->GetString(CHANGE_QB_FAMILY_ID).c_str());
	m_changeP.EnableWindow(FALSE);
	m_changeP.SetWindowText(ILanguageManager::GetLanguageManager()->GetString(CHANGE_QB_PARENT_ID).c_str());
	m_saveT.ShowWindow(SW_HIDE);
	m_saveT.SetWindowText(ILanguageManager::GetLanguageManager()->GetString(SAVE_ID).c_str());
	m_cancelT.ShowWindow(SW_HIDE);
	m_cancelT.SetWindowText(ILanguageManager::GetLanguageManager()->GetString(CANCEL_ID).c_str());
	m_pGLWnd->SetTypeWin(IDialogWnd::INFO_GEN);
	m_pQBWnd->SetTypeWin(IDialogWnd::INFO);
	LoadTree();
	return bRes;
}

void CBQManagerDialog::DoDataExchange(CDataExchange* pDX){
	__super::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_CHANGE_BQ, m_update);
	DDX_Control(pDX, IDC_NEW_BQ, m_new);
	DDX_Control(pDX, IDC_SAVE_BQ, m_save);
	DDX_Control(pDX, IDC_CANCEL_BQ, m_cancel);
	DDX_Control(pDX, IDC_DELETE_BQ, m_delete);
	DDX_Control(pDX, IDC_CHANGE_BQF, m_changeF);
	DDX_Control(pDX, IDC_CHANGE_BQP, m_changeP);
	DDX_Control(pDX, IDC_BQ_TREE, m_qbTree);
	DDX_Control(pDX, IDC_NAME_BQ, m_nameBq);
	DDX_Control(pDX, IDC_SAVE_BQT, m_saveT);
	DDX_Control(pDX, IDC_CANCEL_BQT, m_cancelT);
	DDX_Control(pDX, IDC_CLOSE, m_close);
}

void CBQManagerDialog::LoadTree(){
	m_aItemsAll.clear();
	m_aItems.clear();
	m_qbTree.DeleteAllItems();
	CQueenBeeManager* pQBManager = IJournalComponent::GetQueenBeeManager();
	CFamilyManager* pFamilyManager = IJournalComponent::GetFamilyManager();
	std::map<int, CFamilyManager::point> aPoints = pFamilyManager->GetPoints();
	for(std::map<int, CFamilyManager::point>::iterator it = aPoints.begin(); it != aPoints.end(); it++){
		HTREEITEM item = m_qbTree.InsertItem(it->second.m_strName.c_str());
		m_qbTree.SetItemData(item, MAKELONG(POINT, it->first));
		if(m_type == POINT && it->first == m_nId)
			m_qbTree.SelectItem(item);
		std::map<int, CFamilyManager::family> aFamilies = pFamilyManager->GetFamilies(it->first);
		for(std::map<int, CFamilyManager::family>::iterator it1 = aFamilies.begin(); it1 != aFamilies.end(); it1++){
			HTREEITEM item1 = m_qbTree.InsertItem(it1->second.m_strName.c_str(), item);
			m_qbTree.SetItemData(item1, MAKELONG(FAMILY, it1->first));
			if(m_type == FAMILY && it1->first == m_nId)
				m_qbTree.SelectItem(item1);
			std::map<int, CQueenBeeManager::queen_bee> aQb = pQBManager->GetFamilyQB(it1->first);
			for(std::map<int, CQueenBeeManager::queen_bee>::iterator it2 = aQb.begin(); it2 != aQb.end(); it2++){
				HTREEITEM item2 = m_qbTree.InsertItem(it2->second.s_strName.c_str(), item1);
				m_qbTree.SetItemData(item2, MAKELONG(QUEEN_BEE, it2->first));
				if(m_type == QUEEN_BEE && it2->first == m_nId)
					m_qbTree.SelectItem(item2);
				m_aItems.insert(std::make_pair(it2->first, item2));
			}
			m_qbTree.SortChildren(item1);
		}
		m_qbTree.SortChildren(item);
	}
	m_qbTree.SortChildren(TVI_ROOT);

	HTREEITEM item = m_qbTree.InsertItem(ILanguageManager::GetLanguageManager()->GetString(ALL_ID).c_str(), TVI_ROOT, TVI_FIRST);
	m_qbTree.SetItemData(item, MAKELONG(DEFAULT, 0));
	std::map<int, CQueenBeeManager::queen_bee> aQb = IJournalComponent::GetQueenBeeManager()->GetQueenBees();
	for(std::map<int, CQueenBeeManager::queen_bee>::iterator it = aQb.begin(); it != aQb.end(); it++){
		HTREEITEM item1 = m_qbTree.InsertItem(it->second.s_strName.c_str(), item);
		m_qbTree.SetItemData(item1, MAKELONG(QUEEN_BEE, it->first));
		m_aItemsAll.insert(std::make_pair(it->first, item1));
	}
	m_qbTree.SortChildren(item);
	if(m_type == DEFAULT)
		m_qbTree.SelectItem(m_qbTree.GetFirstVisibleItem());
}
void CBQManagerDialog::OnOK(){}

void CBQManagerDialog::ChangeSelTree(){
	HTREEITEM item = m_qbTree.GetSelectedItem();
	unsigned long value = (unsigned long)m_qbTree.GetItemData(item);
	ChangeSelGL(value);
	ChangeSelQB(value);
}
void CBQManagerDialog::ChangeSelQB(unsigned long value){
	int nId = LOWORD(value) == QUEEN_BEE ? HIWORD(value) : 0;
	if(m_pQBWnd->GetTypeWin() == IDialogWnd::INFO){
		m_pQBWnd->SetId(nId);
		m_update.EnableWindow(nId ? TRUE : FALSE);
		m_delete.EnableWindow(nId ? TRUE : FALSE);
	}
	m_pQBWnd->SetSelectedData(value);
}

void CBQManagerDialog::ChangeSelGL(unsigned long value){
	int nId = 0;
	if(LOWORD(value) == QUEEN_BEE)
		nId = HIWORD(value);
	if(m_pGLWnd->GetTypeWin() == IDialogWnd::INFO_GEN){
		m_pGLWnd->SetId(nId);
		m_changeP.EnableWindow(nId ? TRUE : FALSE);
		m_changeF.EnableWindow(nId ? TRUE : FALSE);
	}
	if(m_pGLWnd->GetTypeWin() == IDialogWnd::CHANGE_F){
		if(LOWORD(value) == POINT || LOWORD(value) == DEFAULT)
			m_saveT.EnableWindow(FALSE);
		else
			m_saveT.EnableWindow(TRUE);
	}
	if(m_pGLWnd->GetTypeWin() == IDialogWnd::CHANGE_P){
		if(LOWORD(value) == QUEEN_BEE && m_pGLWnd->GetId() == HIWORD(value))
			m_saveT.EnableWindow(FALSE);
		else
			m_saveT.EnableWindow(TRUE);
	}
	m_pGLWnd->SetSelectedData(value);
}


BEGIN_MESSAGE_MAP(CBQManagerDialog, CDialog)
	ON_CONTROL(EN_CHANGE, IDC_NAME_BQ, OnChangeName)
	ON_NOTIFY(TVN_SELCHANGED, IDC_BQ_TREE, OnSelChanged)
	ON_NOTIFY(GL_CHANGE_QB, LEFT_WIN_ID, OnSelChanged)
	ON_COMMAND(IDC_CLOSE, OnClose)
	ON_COMMAND(IDC_NEW_BQ, OnNew)
	ON_COMMAND(IDC_CHANGE_BQ, OnChange)
	ON_COMMAND(IDC_DELETE_BQ, OnDelete)
	ON_COMMAND(IDC_SAVE_BQ, OnSave)
	ON_COMMAND(IDC_CANCEL_BQ, OnCancelBQ)
	ON_COMMAND(IDC_CHANGE_BQF, OnChangeF)
	ON_COMMAND(IDC_CHANGE_BQP, OnChangeP)
	ON_COMMAND(IDC_CANCEL_BQT, OnCancelT)
	ON_COMMAND(IDC_SAVE_BQT, OnSaveT)
END_MESSAGE_MAP()

void CBQManagerDialog::OnChangeName(){
	CQueenBeeManager* pQBManager = IJournalComponent::GetQueenBeeManager();
	CString str;
	m_nameBq.GetWindowText(str);
	int nId = pQBManager->FindQueenBee(str.GetString());
	if(nId)
		m_qbTree.SelectItem(m_aItemsAll[nId]);
}

void CBQManagerDialog::OnClose(){
	EndDialog(0);
}

void CBQManagerDialog::OnSelChanged(NMHDR *nmhdr,LRESULT *pResult){
	if(nmhdr->idFrom == IDC_BQ_TREE)
		ChangeSelTree();
	if(nmhdr->idFrom == LEFT_WIN_ID)
		ChangeSelQB(MAKEWPARAM(QUEEN_BEE, ((GLNMHDR*)nmhdr)->m_nParameter));
	pResult = 0;
}

void CBQManagerDialog::OnNew(){
	m_pQBWnd->SetTypeWin(IDialogWnd::NEW);
	m_pQBWnd->SetId(0);
	int nseldata = 0;
	HTREEITEM item = m_qbTree.GetSelectedItem();
	if(item)
		nseldata = m_qbTree.GetItemData(item);
	m_pQBWnd->SetSelectedData(nseldata);
	m_new.ShowWindow(SW_HIDE);
	m_update.ShowWindow(SW_HIDE);
	m_delete.ShowWindow(SW_HIDE);
	m_save.ShowWindow(SW_SHOW);
	m_cancel.ShowWindow(SW_SHOW);
}

void CBQManagerDialog::OnChange(){
	int nId = m_pQBWnd->GetId();
	m_pQBWnd->SetTypeWin(IDialogWnd::CHANGE);
	m_pQBWnd->SetId(nId);
	m_pQBWnd->SetSelectedData(MAKEWPARAM(QUEEN_BEE, nId));
	m_new.ShowWindow(SW_HIDE);
	m_update.ShowWindow(SW_HIDE);
	m_delete.ShowWindow(SW_HIDE);
	m_save.ShowWindow(SW_SHOW);
	m_cancel.ShowWindow(SW_SHOW);
}

void CBQManagerDialog::OnDelete(){
	HTREEITEM item = m_qbTree.GetSelectedItem();
	int nId = m_pQBWnd->GetId();
	std::tstring strContent = ILanguageManager::GetLanguageManager()->GetString(QUESTION_DELETE_ID) +
		_T(" '") + IJournalComponent::GetQueenBeeManager()->GetQueenBee(nId).s_strName + _T("'.");
	if(MessageBox(strContent.c_str(), ILanguageManager::GetLanguageManager()->
		GetString(DELETE_QUEEN_BEE_ID).c_str(), MB_YESNO) == IDYES){
		IJournalComponent::GetQueenBeeManager()->DeleteQueenBee(nId);
		m_qbTree.DeleteItem(m_aItemsAll[nId]);
		m_aItemsAll.erase(nId);
		std::map<int, HTREEITEM>::iterator it = m_aItems.find(nId);
		if(it != m_aItems.end()){
			m_qbTree.DeleteItem(it->second);
			m_aItems.erase(it);
		}
		if((m_pGLWnd->GetTypeWin() == IDialogWnd::CHANGE_F || m_pGLWnd->GetTypeWin() == IDialogWnd::CHANGE_P) && 
			m_pGLWnd->GetId() == nId)
			OnCancelT();
		else
			ChangeSelTree();
	}
}

void CBQManagerDialog::OnSave(){
	IDialogWnd::type nType = m_pQBWnd->GetTypeWin();
	int nId = m_pQBWnd->GetId();
	m_pQBWnd->Save();
	OnCancelBQ();
	std::tstring strName = IJournalComponent::GetQueenBeeManager()->GetQueenBee(nId).s_strName;
	m_qbTree.SetItemText(m_aItemsAll[nId], strName.c_str());
	std::map<int, HTREEITEM>::iterator it = m_aItems.find(nId);
	if(it != m_aItems.end())
		m_qbTree.SetItemText(it->second, strName.c_str());
	if(nType == IDialogWnd::CHANGE)
		ChangeSelTree();
	else if(nType == IDialogWnd::NEW)
		LoadTree();
}

void CBQManagerDialog::OnCancelBQ(){
	m_pQBWnd->SetTypeWin(IDialogWnd::INFO);
	m_new.ShowWindow(SW_SHOW);
	m_update.ShowWindow(SW_SHOW);
	m_delete.ShowWindow(SW_SHOW);
	m_save.ShowWindow(SW_HIDE);
	m_cancel.ShowWindow(SW_HIDE);
	HTREEITEM item = m_qbTree.GetSelectedItem();
	unsigned long value = (unsigned long)m_qbTree.GetItemData(item);
	ChangeSelQB(value);
}

void CBQManagerDialog::OnChangeF(){
	int nId = m_pGLWnd->GetSelectedData();
	m_pGLWnd->SetTypeWin(IDialogWnd::CHANGE_F);
	m_pGLWnd->SetId(nId);
	m_pGLWnd->SetSelectedData(MAKEWPARAM(QUEEN_BEE, nId));
	m_changeF.ShowWindow(SW_HIDE);
	m_changeP.ShowWindow(SW_HIDE);
	m_saveT.ShowWindow(SW_SHOW);
	m_cancelT.ShowWindow(SW_SHOW);
	m_saveT.EnableWindow(TRUE);
}

void CBQManagerDialog::OnChangeP(){
	int nId = m_pGLWnd->GetSelectedData();
	m_pGLWnd->SetTypeWin(IDialogWnd::CHANGE_P);
	m_pGLWnd->SetId(nId);
	HTREEITEM item = m_qbTree.GetSelectedItem();
	unsigned long value = (unsigned long)m_qbTree.GetItemData(item);
	m_pGLWnd->SetSelectedData(MAKEWPARAM(QUEEN_BEE, HIWORD(value)));
	m_changeF.ShowWindow(SW_HIDE);
	m_changeP.ShowWindow(SW_HIDE);
	m_saveT.ShowWindow(SW_SHOW);
	m_cancelT.ShowWindow(SW_SHOW);
	if(HIWORD(value) == nId)
		m_saveT.EnableWindow(FALSE);
	else
		m_saveT.EnableWindow(TRUE);
}

void CBQManagerDialog::OnSaveT(){
	m_pGLWnd->Save();
	OnCancelT();
	LoadTree();
}

void CBQManagerDialog::OnCancelT(){
	m_pGLWnd->SetTypeWin(IDialogWnd::INFO_GEN);
	m_changeF.ShowWindow(SW_SHOW);
	m_changeP.ShowWindow(SW_SHOW);
	m_saveT.ShowWindow(SW_HIDE);
	m_cancelT.ShowWindow(SW_HIDE);
	HTREEITEM item = m_qbTree.GetSelectedItem();
	unsigned long value = (unsigned long)m_qbTree.GetItemData(item);
	ChangeSelGL(value);
}
